<template>
  <div class="login">
    <div class="materialContainer">
      <div class="login-box" v-show="!regIsShow">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
          <h3 class="title">系统登录</h3>
          <el-form-item prop="userName">
            <el-input type="text" v-model="ruleForm.userName" auto-complete="off" placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item prop="passWord">
            <el-input type="password" v-model="ruleForm.passWord" auto-complete="off" placeholder="密码"></el-input>
          </el-form-item>
          <!-- <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox> -->
          <el-form-item style="width:100%;">
            <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit" :loading="logining">登录</el-button>
            <!-- <span @click="showBox">注册</span> -->
          </el-form-item>
        </el-form>
      </div>
      <!-- <div class="register-box" v-show="regIsShow">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
          <h3 class="title">系统注册</h3>
          <el-form-item prop="userName">
            <el-input type="text" v-model="ruleForm.userName" auto-complete="off" placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item prop="passWord">
            <el-input type="password" v-model="ruleForm.passWord" auto-complete="off" placeholder="密码"></el-input>
          </el-form-item>
          <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
          <el-form-item style="width:100%;">
            <el-button type="primary" style="width:100%;" @click.native.prevent="register" :loading="logining">注册</el-button>
            <span @click="showBox">登录</span>
          </el-form-item>
        </el-form>
      </div> -->
    </div>
  </div>
</template>

<script>
  import {login,register} from "@/api/user/api";
  //import NProgress from 'nprogress'
  export default {
    data() {
      return {
        logining: false,
        ruleForm: {
          userName: '',
          passWord: ''
        },
        regIsShow: false, //  登录注册切换显示状态
        rules: {
          userName: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            //{ validator: validaePass }
          ],
          passWord: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            //{ validator: validaePass2 }
          ]
        },
        checked: true
      };
    },
    methods: {
      // 切换登录注册
      showBox (){
        this.regIsShow = !this.regIsShow
      },
      handleSubmit(ev) {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.logining = true;
            let loginParams = { userName: this.ruleForm.userName, passWord: this.ruleForm.passWord };
            // 登录
            login(loginParams).then(res => {
              this.logining = false;
              let { code, data } = res;
              if (code == 200) {
                sessionStorage.setItem('user', JSON.stringify(data));
                this.$router.push({ path: '/company' });
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      // 注册
      register () {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.logining = true;
            let loginParams = { userName: this.ruleForm.userName, passWord: this.ruleForm.passWord };
            // 注册
            register(loginParams).then(res => {
              this.logining = false;
              let { code, data } = res;
              if (code == 200) {
                this.regIsShow = !this.regIsShow
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }

</script>

<style lang="scss" scoped>
  .login{
    background-image: url("../assets/img/login-bg.png");
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
    min-height: 100vh;
    h1 {
      font-size: 2.8em;
      color: #fff;
      letter-spacing: 1px;
      text-align: center;
      text-shadow: 1px 2px 1px rgba(12, 12, 12, 0.37);
      padding: 3vw 1vw;
      font-family: 'Magra', sans-serif;
    }
    .materialContainer {
      width: 100%;
      max-width: 460px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      .reg{
        color: #fff;
        margin: 0 20px;
      }
    }

  }
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      color: #505458;
    }
    .remember {
      margin: 0px 0px 35px 0px;
    }
  }
</style>
